import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { LayerBase } from './LayerBase';
import { UserInfo } from './UserInfo';
import { randomNum } from '../utils/utils';
import { EverydayTaskItem } from './EverydayTaskItem';
import { StartSceneGameMgr } from './StartSceneGameMgr';
const { ccclass, property } = _decorator;

@ccclass('EveryDayTaskPanel')
export class EveryDayTaskPanel extends LayerBase {
    
    @property(Prefab)
    everyDayTaskItemPre:Prefab;
    @property(Node)
    content:Node;

    everyDayTaskTotalNum=4;

    init(somedData?: any): void {
        this.content.removeAllChildren();
        if (UserInfo.todayEveryDayTask) {//如果今日任务已经获取
            UserInfo.todayEveryDayTask.forEach(element => {
                    let pre = instantiate(this.everyDayTaskItemPre);
                    pre.setParent(this.content);
                    pre.getComponent(EverydayTaskItem).init(element);
            });
        }
        else{//没有获取
            console.log("没有获取")
            let AllEveryDayTaskCongfiArr=[];
            let resEveryDayTaskCongfiArr=[]
            UserInfo.everyDayTaskCongfiDic.getValueList().forEach((ele)=>{
                AllEveryDayTaskCongfiArr.push(ele);
            })
    
            // for (let i = 0; i < this.everyDayTaskTotalNum; i++) {
            //     let res=AllEveryDayTaskCongfiArr[randomNum(0,AllEveryDayTaskCongfiArr.length-1)];
    
            //     if (AllEveryDayTaskCongfiArr.indexOf(res)) {
            //         AllEveryDayTaskCongfiArr.splice(AllEveryDayTaskCongfiArr.indexOf(res),1)
            //     }
    
            //     resEveryDayTaskCongfiArr.push(res);
    
            // }
            setTimeout(()=>{
                console.log(AllEveryDayTaskCongfiArr,"AllEveryDayTaskCongfiArr")
                while (resEveryDayTaskCongfiArr.length < this.everyDayTaskTotalNum) {
                    let res = AllEveryDayTaskCongfiArr[randomNum(0, AllEveryDayTaskCongfiArr.length - 1)];
    
                    let isRepeat = false;
                    resEveryDayTaskCongfiArr.forEach((ele) => {
                        if (res.everyTaskType == ele.everyTaskType) {
                            isRepeat=true;
                        }
                    })
    
                    if (isRepeat) {
                        continue;
                    }
    
                    if (AllEveryDayTaskCongfiArr.indexOf(res)) {
                        AllEveryDayTaskCongfiArr.splice(AllEveryDayTaskCongfiArr.indexOf(res), 1)
                    }
                    resEveryDayTaskCongfiArr.push(res);
                }
    
                console.log(resEveryDayTaskCongfiArr, "resEveryDayTaskCongfiArr")
        
                for (let t = 0; t < this.everyDayTaskTotalNum; t++) {
                    let pre = instantiate(this.everyDayTaskItemPre);
                    pre.setParent(this.content);
                    pre.getComponent(EverydayTaskItem).init(resEveryDayTaskCongfiArr[t]);
                }
                UserInfo.todayEveryDayTask=resEveryDayTaskCongfiArr;
                StartSceneGameMgr.ins.saveGameDate();
            },100)

        }
      

    }

}


